﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Data;

namespace ReklamaWPF
{
    /// <summary>
    /// Interaction logic for AdvertisingAdd.xaml
    /// </summary>
    public partial class AdvertisingAdd : Window, INotifyPropertyChanged
    {
        
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(name));
          }
      }

        public bool red = false;

        private string _advid;
        public string AdvId
        {
            get { return _advid; }
            set
            {
                if (value != _advid)
                {
                    _advid = value;
                    OnPropertyChanged("AdvId");
                }
            }
        }
        private string _clipid;
        public string ClipId
        {
            get { return _clipid; }
            set
            {
                if (value != _clipid)
                {
                    _clipid = value;
                    OnPropertyChanged("ClipId");
                }
            }
        }

        private string _progid;
        public string ProgId
        {
            get { return _progid; }
            set
            {
                if (value != _progid)
                {
                    _progid = value;
                    OnPropertyChanged("ProgId");
                }
            }
        }

        private string _comid;
        public string ComId
        {
            get { return _comid; }
            set
            {
                if (value != _comid)
                {
                    _comid = value;
                    OnPropertyChanged("ComId");
                }
            }
        }


        public AdvertisingAdd()
        {
            InitializeComponent();
             addAdvForm.DataContext = this;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!red)
                {
                    if (Functionality.insertAdvIntoDB(this))
                    {
                        DialogResult = true;
                        Close();
                    }
                    else
                    {
                        MessageBox.Show("Klaida įvedamuose duomenyse!");
                    }
                }
                else
                {
                    DialogResult = true;
                    Close();
                }

            }
            catch (Exception ex) { MessageBox.Show("Klaida įvedamuose duomenyse!"); return; }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        private void textBox6_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SelectId idf = new SelectId();
            idf.LoadData("select id, Clip_Name AS 'Name' from Clips");
            if (idf.ShowDialog() == true)
            {
                ClipId = idf.Id;
            }
        }

        private void textBox1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Advertiser
            SelectId idf = new SelectId();
            idf.LoadData("select id, Advertiser_Name AS 'Name' from Advertisers");
            if (idf.ShowDialog() == true)
            {
                    AdvId = idf.Id;
            }
        }

        private void textBox7_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SelectId idf = new SelectId();
            if ((AdvId == "") || (AdvId == null))  { AdvId = "0"; }
            idf.LoadData("select id, Campaign_Name AS 'Name' from Campaigns where Advertiser_id="+AdvId);
            if (idf.ShowDialog() == true)
            {
                ComId = idf.Id;
            }
        }

        private void textBox8_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {   
            SelectId idf = new SelectId();
            idf.LoadData("select id, [Lietuviškas pavadinimas] AS 'Name' from Archyvas$");
            if (idf.ShowDialog() == true)
            {
                ProgId = idf.Id;
            }
        }
    }

    public class AdverFromIDConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return "";
            }
            else
            {
                DataTable tb=Functionality.db.getTable("select Advertiser_Name from Advertisers where id=" + value.ToString());
                if (tb.Rows.Count > 0)
                {
                    return tb.Rows[0][0].ToString();
                }
                else return "";
            }
            //return "10";
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return value.ToString();
            }
            else
            {
                return "";
            }
        }
    }

    public class CompaignFromIDConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return "";
            }
            else
            {
                DataTable tb = Functionality.db.getTable("select Campaign_Name from Campaigns where id=" + value.ToString());
                if (tb.Rows.Count > 0)
                {
                    return tb.Rows[0][0].ToString();
                }
                else return "";
                 
            }
            //return "10";
        }

        public object ConvertBack(object value, Type targetType,
           object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return value.ToString();
            }
            else
            {
                return "";
            }
        }
    }

    public class ProgFromIDConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return "";
            }
            else
            {
                DataTable tb = Functionality.db.getTable("select [Lietuviškas pavadinimas] from Archyvas$ where id=" + value.ToString());
                if (tb.Rows.Count > 0)
                {
                    return tb.Rows[0][0].ToString();
                }
                else return "";
            }
            //return "10";
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return value.ToString();
            }
            else
            {
                return "";
            }
        }
    }

    public class ClipFromIDConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return "";
            }
            else
            {
                DataTable tb = Functionality.db.getTable("select Clip_Name from Clips where id=" + value.ToString());
                if (tb.Rows.Count > 0)
                {
                    return tb.Rows[0][0].ToString();
                }
                else return "";
            }
            //return "10";
        }

        public object ConvertBack(object value, Type targetType,
           object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return value.ToString();
            }
            else
            {
                return "";
            }
        }
    }

}
